<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			html,body{ background: linear-gradient(#23252d 3%, #2a343f 97%);height:100%;  }
			.top { padding-top:110px; text-align:center; }
			.top img { margin:0 auto; width:70px; border-radius: 7px; }
			.top b {display:block; font-size: 18px; font-weight: 500; color:#fff; line-height: 30px; padding-top: 7px;}
			.top p {display:block; font-size: 14px; font-weight: 400; color: #e2e3e6; line-height: 25px; }
			.footer { position: fixed; left: 0; right: 0; bottom: 45px; display:flex; }
			.footer div { width:100%; border:0px red solid; text-align: center;}
			.footer img { width:55px; margin:0 auto; }
			.footer div p { font-size:12px; color:#fff; line-height:40px;}
			.display-none{ display:none; }
			.display-none{ display:none; }
			.text-center { text-align: center !important; }
			
		</style>
	</head>
	<body>
		<div class="top">
			<img src="../../image/defaultimg.png" id="avatar">
			<b id="nickname">--</b>
			<p id="status">正在等待对方接受邀请...</p>
		</div>
		<div class="footer">
			
			<div tapmode onclick="hangup();">
				<img src="../../image/refuse.png" />
				<p>挂断</p>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var rong;
		var myuid,dfuid,dfnickname,dfavatar,mediaType;
		apiready = function() {
			api.parseTapmode();
			api.bringFrameToFront({
				from: api.frameName
			});
			
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					myuid = userinfo.mobile;
				}
			});
			
			
			rong = api.require('rongCloud2');
			dfuid = api.pageParam.dfuid;
			dfnickname = api.pageParam.dfnickname;
			dfavatar = api.pageParam.dfavatar;
			mediaType = api.pageParam.mediaType;
			if(dfavatar != '') {
				$api.byId('avatar').src = dfavatar;
			}
			$api.text($api.byId('nickname'),dfnickname);
			//playMP3();
			rong.startCall({
				targetId:dfuid,
				mediaType:mediaType,
				conversationType:'PRIVATE',
				extra:'',
				userIdList: [dfuid]
			},function(ret){
				console.log('startCall:' + JSON.stringify(ret))
				var callId = ret.callSession.callId;
				console.log(callId);
				//fnsetVideoView(0, myuid); 
				
			});
			api.addEventListener({
				name : 'rongclouddisconn'
			}, function(ret, err) {
				disConn(ret.value.reason);
				
			});
			api.addEventListener({
				name : 'rongclouddidconn'
			}, function(ret, err) {
				//已接通
				//fnsetVideoView(40, myuid); 
			});
		}
		function fnsetVideoView(y, uid) {
			console.log(uid);
			rong.setVideoView({
				rect: {
					x: 0,
					y: y,
					w: 320,
					h: 220
				},
				userId: uid,
				bg: '#ff0000',
				renderModel: 'fit',
				//fixedOn: api.frameName,
				fixed: true
			});
		}
		function disConn(reason) {
			//断开连接
			api.stopPlay();
			var reasonstr = '';
			if(reason == 12) {
				reasonstr = '对方已挂断';
			}
			else if(reason == 15) {
				reasonstr = '对方未接听';
			}
			else {
				reasonstr = '连接错误:'+reason;
			}
			$api.text($api.byId('status'),reasonstr);
			api.startPlay({
				path: 'widget://res/dong.wav'
			}, function(ret, err) {
				if (ret) {
					setTimeout(function(){
						closeframe();
					}, 1000);
					
				} else {
					//alert(JSON.stringify(err));
				}
			});
		}
		function closeframe() {
			api.closeFrame({
				name: api.frameName
			});
		}
		function playMP3() {
			api.startPlay({
				path: 'widget://res/wxring.mp3'
			}, function(ret, err) {
				if (ret) {
					playMP3();
				} else {
					//alert(JSON.stringify(err));
				}
			});
		}
		function hangup() {
			//挂断
			rong.hangup();
			api.stopPlay();
			api.startPlay({
				path: 'widget://res/dong.wav'
			}, function(ret, err) {
				if (ret) {
					setTimeout(function(){
						closeframe();
					}, 1000);
					
				} else {
					//alert(JSON.stringify(err));
				}
			});
		}
	</script>
</html>